home *** CD-ROM | disk | FTP | other *** search
/ Merciful 5 / Merciful - Disc 5.iso / software / p / pcqpascalv1.2d.lha / Examples2 / RemoveChunk / RemoveChunk.p
Encoding:
Text File  |  1997-05-06  |  2.4 KB  |  113 lines

  1. Program RemoveChunk;
  2.  
  3. { Utility, um ueberfluessige Chunks (z.B. GRAB, CRNG, AUTH)
  4.   aus IFF-Datei zu entfernen.
  5.  
  6.   Autor: Andreas Tetzl
  7.  
  8.   Public Domain
  9. }
  10.  
  11. {$I "Include:Exec/Memory.i"}
  12. {$I "Include:DOS/DOS.i"}
  13. {$I "Include:Utils/StringLib.i"}
  14. {$I "Include:Utils/Parameters.i"}
  15.  
  16. const Version = "$VER: RemoveChunk 1.0 (18.2.96) Andreas Tetzl";
  17.  
  18. VAR source, dest : FileHandle;
  19.     buffer : String;
  20.     chunkbuf : Address;
  21.     sourcename, destname, chunk : String;
  22.     i, s : Integer;
  23.     chunkfound : Boolean;
  24.  
  25. FUNCTION MAKE_ID(Str : String) : Integer;
  26. BEGIN
  27.  MAKE_ID:=(Ord(Str[0]) shl 24) OR (Ord(Str[1]) shl 16) OR (Ord(Str[2]) shl 8) OR (Ord(Str[3]));
  28. END;
  29.  
  30.  
  31. BEGIN
  32.  chunkfound:=FALSE;
  33.  sourcename:=AllocString(500);
  34.  destname:=AllocString(500);
  35.  chunk:=AllocString(20);
  36.  buffer:=AllocString(20);
  37.  
  38.  GetParam(1, sourcename);
  39.  GetParam(2, destname);
  40.  GetParam(3, chunk);
  41.  
  42.  if StrEq(sourcename,"") or StrEq(destname,"") or StrEq(chunk,"") then
  43.   BEGIN
  44.    Writeln("Usage: RemoveChunk Source Destination IFFChunkName");
  45.    Exit(0);
  46.   END;
  47.  
  48.  source:=DOSOpen(sourcename,MODE_OLDFILE);
  49.  if source=NIL then
  50.   BEGIN
  51.    writeln("can't open source file");
  52.    Exit(10);
  53.   END;
  54.  
  55.  i:=DOSRead(source,buffer,4);
  56.  if MAKE_ID(buffer)<>MAKE_ID("FORM") then
  57.   BEGIN
  58.    DOSClose(source);
  59.    writeln("no iff file");
  60.    Exit(10);
  61.   END;
  62.  
  63.  dest:=DOSOpen(destname,MODE_NEWFILE);
  64.  if dest=NIL then
  65.   BEGIN
  66.    DOSClose(source);
  67.    writeln("can't open destination file");
  68.    Exit(10);
  69.   END;
  70.  
  71.  i:=DOSWrite(dest,buffer,4);
  72.  i:=DOSRead(source,buffer,8);  { read filesize, filetype }
  73.  i:=DOSWrite(dest,buffer,8);
  74.  
  75.  Repeat
  76.   i:=DOSRead(source,buffer,4);     { read chunk name }
  77.   i:=DOSRead(source,adr(s),4);     { read chunk size }
  78.  
  79.   if i=0 then   { end of file }
  80.    BEGIN
  81.     DOSClose(source);
  82.     DOSClose(dest);
  83.     if chunkfound then
  84.      Writeln("chunk ",chunk," removed")
  85.     else
  86.      Writeln("chunk ",chunk," not found");
  87.     Exit(0);
  88.    END;
  89.  
  90.   chunkbuf:=AllocVec(s,MEMF_ANY);
  91.   if chunkbuf=NIL then
  92.    BEGIN
  93.     DOSClose(source);
  94.     DOSClose(dest);
  95.     Writeln("not enough memory");
  96.     Exit(20);
  97.    END;
  98.  
  99.   i:=DOSRead(source,chunkbuf,s);     { read chunk data }
  100.  
  101.   if MAKE_ID(buffer)<>MAKE_ID(chunk) then
  102.    BEGIN
  103.     i:=DOSWrite(dest,buffer,4);     { write chunk name }
  104.     i:=DOSWrite(dest,adr(s),4);     { write chunk size }
  105.     i:=DOSWrite(dest,chunkbuf,s);   { write chunk data }
  106.    END
  107.   ELSE chunkfound:=TRUE;
  108.  
  109.   FreeVec(ChunkBuf);
  110.  Until FALSE;
  111. END.
  112.  
  113.